package com.eco.fanliapp.service;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.eco.fanliapp.common.constant.ApiEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class JpushMessage {

    private static final Logger log = LoggerFactory.getLogger(JpushMessage.class);

    /**
     * 透传消息
     *
     * @param type    推送类型
     * @param account 推送账户
     * @param data    json数据集
     */
    public static void jiGuangPush(int type, String account, String data) {
        JPushClient jpushClient = new JPushClient(ApiEnum.JG_MASTER_SECRET,
                ApiEnum.JG_APP_KEY);
        PushPayload payload = PushPayload
                .newBuilder()
                .setPlatform(Platform.all())
                //.setAudience(Audience.all())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.alert(111))
                .setMessage(Message.newBuilder().setContentType(String.valueOf(type)).setMsgContent(data).build())
                .setOptions(Options.newBuilder().build()).build();
        log.info("推送的数据" + data);
        PushResult result = null;
        try {
            result = jpushClient.sendPush(payload);
            log.info("result数据：--" + result);
        } catch (Exception e) {
            log.error("jiGuangPush推送错误--" + e.getMessage());
        }
        jpushClient.close();
    }

    /**
     * 在用 Android
     * 通知消息
     *
     * @param account
     * @param title
     */
    public static void jiGuangPushMesageAndroid(int type, String title, String account, String data) {
        JPushClient jpushClient = new JPushClient(ApiEnum.JG_MASTER_SECRET,
                ApiEnum.JG_APP_KEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .setAlert(data)
                                .addExtra("type", type)
                                .addExtra("jumpType", type)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(ApiEnum.JG_APNS_PRODUCT)
                        .build())
                .build();
        PushResult result = null;
        try {
            log.info("payload数据：--" + payload);
            result = jpushClient.sendPush(payload);
            log.info("result数据：--" + result);
        } catch (Exception e) {
            log.error("jiGuangPushMesageAndroid推送错误--" + e.getMessage());
        }
        jpushClient.close();
    }

    /**
     * 在用 ios
     * 通知消息
     *
     * @param account
     * @param title
     */
    public static void jiGuangPushMesageIos(int type, String title, String account, String data) {
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(title, null, data).build();
        JPushClient jpushClient = new JPushClient(ApiEnum.JG_MASTER_SECRET,
                ApiEnum.JG_APP_KEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)
//                                .setContentAvailable(true)//静默推送
                                .addExtra("data", data)
                                .addExtra("type", type)
                                .addExtra("jumpType", type)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(ApiEnum.JG_APNS_PRODUCT)
                        .build())
                .build();
        PushResult result = null;
        try {
            log.info("payload数据：--" + payload);
            result = jpushClient.sendPush(payload);
            log.info("result数据：--" + result);
        } catch (Exception e) {
            log.error("jiGuangPushMesageIos--" + e.getMessage());
        }
        jpushClient.close();
    }

    /**
     * 在用 Android
     * 通知消息
     *
     * @param account
     * @param title
     */
    public static void jiGuangPushMesageListAndroid(int type, String title, List<String> account, String data, Integer jumpType, String jumpLink, Integer jumpTaobao) {
        JPushClient jpushClient = new JPushClient(ApiEnum.JG_MASTER_SECRET,
                ApiEnum.JG_APP_KEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(data)
                                .setTitle(title)
                                .addExtra("type", type)
                                .addExtra("jumpType", jumpType)
                                .addExtra("jumpLink", jumpLink)
                                .addExtra("jumpTaobao", jumpTaobao)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(ApiEnum.JG_APNS_PRODUCT)
                        .build())
                .build();
        PushResult result = null;
        try {
            log.info("payload数据：--" + payload);
            result = jpushClient.sendPush(payload);
            log.info("result数据：--" + result);
        } catch (Exception e) {
            log.error("jiGuangPushMesageListAndroid--" + e.getMessage());
        }
        jpushClient.close();
    }

    /**
     * 在用 ios
     * 通知消息
     *
     * @param account
     * @param title
     */
    public static void jiGuangPushMesageListIos(int type, String title, List<String> account, String data, Integer jumpType, String jumpLink, Integer jumpTaobao) {
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(title, null, data).build();
        JPushClient jpushClient = new JPushClient(ApiEnum.JG_MASTER_SECRET,
                ApiEnum.JG_APP_KEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(account))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)
//                                .setContentAvailable(true)//静默推送
                                .addExtra("type", type)
                                .addExtra("jumpType", jumpType)
                                .addExtra("jumpLink", jumpLink)
                                .addExtra("jumpTaobao", jumpTaobao)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(ApiEnum.JG_APNS_PRODUCT)
                        .build())
                .build();
        PushResult result = null;
        try {
            log.info("payload数据：--" + payload);
            result = jpushClient.sendPush(payload);
            log.info("result数据：--" + result);
        } catch (Exception e) {
            log.error("jiGuangPushMesageListIos--" + e.getMessage());
        }
        jpushClient.close();
    }


    public static void main(String[] args) {
//    	String str = "{\"createTime\":1558059245869,\"iconUrl\":\"http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZKpebjp57C1V2PiaZ702Gdv9pm9ibg65uVeKZ0QRfsca6Z9vr0WKIBkf4vd4ykHZEOkNAPwnxCkLg/132\",\"newsContent\":\"微信授权成功奖励已到账\",\"newsTitle\":\"微信授权\",\"newsType\":4,\"tableId\":313,\"updateTime\":1558059245869,\"userId\":176102687}";
//    	String str2 = "{\"notification\":{\"ios\":{\"alert\":{\"title\":\"JPush Title\",\"subtitle\":\"JPush Subtitle\"},\"sound\":\"sound.caf\",\"badge\":1,\"extras\":{\"news_id\":134,\"my_key\":\"a value\"}}}}";
//    	//jiGuangPushMesage(4, "1771714693", str2);
//    	String title = "微信授权成功";
//    	Map<String, String> map = new HashMap<String,String>();
//    	map.put("type", "1000000");
//    	map.put("title", title);
//    	map.put("con", "微信授权成功微信授权成功微信授权成功微信授权成功微信授权成功微信授权成功");

        //getIos(title,map,"微信授权成功奖励已到账", "1771714693");
        //jiGuangPushMesage(111, "1771714693", "微信授权成功微信授权成功微信授权成功微信授权成功微信授权成功微信授权成功");
        //getIos(title, "msg_"+title, "msg_content", "1000");
        jiGuangPushMesageIos(502, "我是标题", "601276602437", "我是消息内容");
//        jiGuangPushMesageAndroid(111, "安卓测试6", "1771714693", "");
    }
}
